package com.wenhui.schoolyardinfo.message.ui;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.base.BaseFragment;
import com.wenhui.schoolyardinfo.callback.RcvOnItemViewClickListener;
import com.wenhui.schoolyardinfo.callback.RcvOnLongItemTripletsViewClickListener;
import com.wenhui.schoolyardinfo.customview.CommonPopupWindow;
import com.wenhui.schoolyardinfo.customview.shadowview.ShadowProperty;
import com.wenhui.schoolyardinfo.customview.shadowview.ShadowViewDrawable;
import com.wenhui.schoolyardinfo.firstpage.adapter.BaseRecyclerAdapter;
import com.wenhui.schoolyardinfo.firstpage.holder.SmartViewContentHolder;
import com.wenhui.schoolyardinfo.firstpage.ui.HomePageActivity;
import com.wenhui.schoolyardinfo.manager.PopupWindowManager;
import com.wenhui.schoolyardinfo.manager.ScreenManager;
import com.wenhui.schoolyardinfo.message.adapter.NoticeAdapter;
import com.wenhui.schoolyardinfo.message.adapter.PrivateLetterAdapter;
import com.wenhui.schoolyardinfo.message.bean.NoticeBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/6/26 11:21
 * introduce : 通知Fragment
 */

public class NoticeFragment extends BaseFragment implements RcvOnItemViewClickListener, RcvOnLongItemTripletsViewClickListener {

    @BindView(R.id.mine_recycler_view)
    RecyclerView mineRecyclerView;
    Unbinder unbinder;

    private List<NoticeBean> noticeBeanList;
    private NoticeAdapter noticeAdapter;
    private LinearLayoutManager layoutManager;
    //Popwindow
    private CommonPopupWindow window;
    private RecyclerView moreRecyclerView;
    private RelativeLayout layoutMore;
    private BaseRecyclerAdapter<String> baseRecyclerAdapter;
    private List<String> stringList;
    private boolean isPopupWindowShow;
    private View layoutChatMessage;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_notice, container, false);
        unbinder = ButterKnife.bind(this, view);

        initData();
        initViews();
        return view;
    }

    @Override
    protected void initData() {
        noticeBeanList = new ArrayList<>();
        noticeBeanList.add(new NoticeBean("http://ww1.sinaimg.cn/large/0065oQSqly1fsfq1k9cb5j30sg0y7q61.jpg",
                "埃里希·冯·曼施坦因", "今天天气不错。", 10000010101010L, true, false));
        noticeBeanList.add(new NoticeBean("http://ww1.sinaimg.cn/large/0065oQSqly1fsfq1k9cb5j30sg0y7q61.jpg",
                "埃里希·冯·曼施坦因", "今天天气不错。", 10000010101010L, false, true));
        noticeBeanList.add(new NoticeBean("http://ww1.sinaimg.cn/large/0065oQSqly1fsfq1k9cb5j30sg0y7q61.jpg",
                "埃里希·冯·曼施坦因", "今天天气不错。", 8000010101010L, false, true));
        noticeBeanList.add(new NoticeBean("http://ww1.sinaimg.cn/large/0065oQSqly1fsfq1k9cb5j30sg0y7q61.jpg",
                "埃里希·冯·曼施坦因", "今天天气不错。", 7500010101010L, false, false));
        noticeBeanList.add(new NoticeBean("http://ww1.sinaimg.cn/large/0065oQSqly1fsfq1k9cb5j30sg0y7q61.jpg",
                "埃里希·冯·曼施坦因", "今天天气不错。", 7600010101010L, false, false));
        noticeBeanList.add(new NoticeBean("http://ww1.sinaimg.cn/large/0065oQSqly1fsfq1k9cb5j30sg0y7q61.jpg",
                "埃里希·冯·曼施坦因", "今天天气不错。", 8100010101010L, false, false));
        noticeBeanList.add(new NoticeBean("http://ww1.sinaimg.cn/large/0065oQSqly1fsfq1k9cb5j30sg0y7q61.jpg",
                "埃里希·冯·曼施坦因", "今天天气不错。", 7100010101010L, false, false));
        noticeBeanList.add(new NoticeBean("http://ww1.sinaimg.cn/large/0065oQSqly1fsfq1k9cb5j30sg0y7q61.jpg",
                "埃里希·冯·曼施坦因", "今天天气不错。", 7100010101010L, false, false));

        stringList = new ArrayList<>();
        stringList.add("删除");
        stringList.add("取消");
        isPopupWindowShow = false;
    }

    @Override
    protected void initViews() {

        initAdapter();
        initPopupWindow();
    }

    private void initAdapter() {
        layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        mineRecyclerView.setLayoutManager(layoutManager);
        mineRecyclerView.setItemAnimator(new DefaultItemAnimator());
//        mineRecyclerView.addItemDecoration(new RecyclerViewDivider(
//                getActivity(),
//                OrientationHelper.VERTICAL,
//                dividerHeight,
//                dividerColor));

        noticeAdapter = new NoticeAdapter(getActivity());
        mineRecyclerView.setAdapter(noticeAdapter);
        noticeAdapter.setRcvOnItemViewClickListener(this);
        noticeAdapter.setRcvOnLongItemTripletsViewClickListener(this);

        noticeAdapter.clearData();
        noticeAdapter.addAllData(noticeBeanList);
    }

    private void initPopupWindow() {
        // create popup window
        window = new CommonPopupWindow(getActivity(), R.layout.popup_window_private_letter, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
            @Override
            protected void initViews() {
                View view = getContentView();
                layoutManager = new LinearLayoutManager(getActivity());
                layoutManager.setOrientation(OrientationHelper.VERTICAL);

                moreRecyclerView = view.findViewById(R.id.more_recycler_view);
                layoutMore = view.findViewById(R.id.layout_more);
                moreRecyclerView.setItemAnimator(new DefaultItemAnimator());
                moreRecyclerView.setLayoutManager(layoutManager);
//                moreRecyclerView.addItemDecoration(new RecyclerViewDivider(
//                        getActivity(),
//                        LinearLayoutManager.VERTICAL,
//                        dividerHeight,
//                        dividerColor));
                ShadowProperty shadowProperty = new ShadowProperty()
                        .setShadowColor(getResources().getColor(R.color.colorGrayF))
                        .setShadowDx(ScreenManager.dipTopx(getActivity(), 2))
                        .setShadowDy(ScreenManager.dipTopx(getActivity(), 2))
                        .setShadowRadius(ScreenManager.dipTopx(getActivity(), 8))
                        .setShadowSide(ShadowProperty.TOP | ShadowProperty.LEFT | ShadowProperty.RIGHT | ShadowProperty.BOTTOM);
                ShadowViewDrawable shadowViewDrawable = new ShadowViewDrawable(shadowProperty, Color.WHITE, 0, 0);
                ViewCompat.setBackground(layoutMore, shadowViewDrawable);
                ViewCompat.setLayerType(layoutMore, ViewCompat.LAYER_TYPE_SOFTWARE, null);

                baseRecyclerAdapter = new BaseRecyclerAdapter<String>(R.layout.item_popup_window_recommend, 1) {
                    @Override
                    protected void onBindViewHolder(RecyclerView.ViewHolder holder, String model, int position) {
                        if (holder instanceof SmartViewContentHolder) {
                            SmartViewContentHolder smartViewContentHolder = (SmartViewContentHolder) holder;
                            smartViewContentHolder.text(R.id.tev_concern, model);

                            View viewConcern = smartViewContentHolder.getView(R.id.view_concern);
                            if (position == stringList.size() - 1) {
                                viewConcern.setVisibility(View.GONE);
                            } else {
                                viewConcern.setVisibility(View.VISIBLE);
                            }
                        }
                    }
                };

                moreRecyclerView.setAdapter(baseRecyclerAdapter);
                baseRecyclerAdapter.clearData();
                baseRecyclerAdapter.addAllData(stringList);
            }

            @Override
            protected void initEvent() {
                baseRecyclerAdapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        switch (position) {
                            case 0:
                                showToast("点击了concern");
                                break;
                            default:
                                break;
                        }
                        window.getPopupWindow().dismiss();
                    }
                });
            }

            @Override
            protected void initWindow() {
                super.initWindow();
                PopupWindow instance = getPopupWindow();
                instance.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
//                        lp.alpha = 1.0f;
                        getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                        getActivity().getWindow().setAttributes(lp);
                    }
                });
            }
        };
    }

    private void showPopupWindow(View anchorView, View anchorView2) {
//        View contentView = CommonPopupWindow();
//        mPopupWindow = new PopupWindow(contentView,
//                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        // 如果不设置PopupWindow的背景，有些版本就会出现一个问题：无论是点击外部区域还是Back键都无法dismiss弹框
//        mPopupWindow.setBackgroundDrawable(new ColorDrawable());
//        // 设置好参数之后再show
//        int windowPos[] = PopupWindowManager.calculatePopWindowPos(anchorView, contentView);
//        int xOff = 20; // 可以自己调整偏移
//        windowPos[0] -= xOff;
//        mPopupWindow.showAtLocation(anchorView, Gravity.TOP | Gravity.START, windowPos[0], windowPos[1]);

        final PopupWindow mPopupWindow = window.getPopupWindow();
        mPopupWindow.setAnimationStyle(R.style.animTranslate);
        // 如果不设置PopupWindow的背景，有些版本就会出现一个问题：无论是点击外部区域还是Back键都无法dismiss弹框
        mPopupWindow.setBackgroundDrawable(new ColorDrawable());
        // 设置点击窗口外边窗口消失
        mPopupWindow.setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (isPopupWindowShow) {
                    isPopupWindowShow = false;
                    layoutChatMessage.setBackgroundColor(getResources().getColor(R.color.colorWhiteB));
                }
            }
        });

        // 设置好参数之后再show
        int windowPos[] = PopupWindowManager.calculatePopWindowShadowedPos(anchorView, window.getContentView());
        int xOff = 20; // 可以自己调整偏移
        windowPos[0] -= xOff;

        //是否需要向上弹出/向下弹出显示
        boolean isNeedShowUp = PopupWindowManager.isNeedShowUp(anchorView, window.getContentView());
        if (isNeedShowUp) {
            // 设置popupWindow的显示位置
            mPopupWindow.showAtLocation(anchorView, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, windowPos[1]);
        } else {
            // 设置popupWindow的显示位置
            mPopupWindow.showAtLocation(anchorView, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, windowPos[1]);
        }
        isPopupWindowShow = true;
//        // 设置popupWindow的显示位置，此处是在手机屏幕底部且水平居中的位置
//        mPopupWindow.showAtLocation(anchorView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
//        lp.alpha = 0.3f;
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getActivity().getWindow().setAttributes(lp);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onItemClickListener(int position, View view) {
        switch (view.getId()) {
            case R.id.imv_head_portrait:
                startActivity(HomePageActivity.class);
                break;
            case R.id.tev_concern_add:
                showToast("tev_concern_add");
                break;
            case R.id.layout_notice:
                showToast("layout_notice");
                break;
            default:
                break;
        }
    }

    @Override
    public void onLongItemClickListener(int position, View view, View view2, View view3) {
        switch (view.getId()) {
            case R.id.layout_notice:
                view.setBackgroundColor(getResources().getColor(R.color.colorGrayC));
                layoutChatMessage = view;
                showPopupWindow(view2, view3);
                break;
            default:
                break;
        }
    }
}
